﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using BI.CF.Model;
using BI.CF.IDAL;
using System.Data.SqlClient;

namespace BI.CF.DAL
{
    public class RepayDetailedDAL:RepayDetailedIDAL
    {
        /// <summary>
        /// 根据ID显示还款信息
        /// </summary>
        /// <param name="Mid"></param>
        /// <returns></returns>
        public PageList.Pager<RepayDetailedModel> ShowRepayDetailed(string TimeMin, string TimeMax, int Mid, int Uid ,int PageIndex, int PageSize)
        {
            SqlParameter[] parameter = {                                            
                                           new SqlParameter("@TimeMin",SqlDbType.VarChar),
                                           new SqlParameter("@TimeMax",SqlDbType.VarChar),
                                           new SqlParameter("@MemInvestId", SqlDbType.Int), 
                                           new SqlParameter("@UserId",SqlDbType.Int),
                                           new SqlParameter("@PageIndex",SqlDbType.Int),
                                           new SqlParameter("@PageSize",SqlDbType.Int)
                                       };
            parameter[0].Value = TimeMin;
            parameter[1].Value = TimeMax;
            parameter[2].Value = Mid;
            parameter[3].Value = Uid;
            parameter[4].Value = PageIndex;
            parameter[5].Value = PageSize;
            List<RepayDetailedModel> list = new List<RepayDetailedModel>();
            int totalCount = 0;
            DataTable dt = DbHelperSQL.RunProcedure("PROC_RepayDetailedShow", parameter, PageSize, PageIndex, out totalCount);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                RepayDetailedModel model = new RepayDetailedModel();
                model.RepayDetailedId = Convert.ToInt32(dt.Rows[i]["RepayDetailedId"].ToString());
                model.MemInvestId = Convert.ToInt32(dt.Rows[i]["MemInvestId"].ToString());
                model.SumDay = dt.Rows[i]["SumDay"].ToString();
                model.P_RepaymentTime = dt.Rows[i]["P_RepaymentTime"].ToString();
                model.R_RepaymentTime = dt.Rows[i]["R_RepaymentTime"].ToString();
                model.OverdueForfeit = Convert.ToDecimal(dt.Rows[i]["OverdueForfeit"].ToString());
                model.RushFee = Convert.ToDecimal(dt.Rows[i]["RushFee"].ToString());
                model.RepaymentMoney = Convert.ToDecimal(dt.Rows[i]["RepaymentMoney"].ToString());
                model.PaymentState = dt.Rows[i]["PaymentState"].ToString();
                model.Title = dt.Rows[i]["Title"].ToString();
                model.RepaymentDay = Convert.ToDateTime(dt.Rows[i]["RepaymentDay"].ToString());
                model.RepaymentModeId = dt.Rows[i]["RepaymentModeId"].ToString();
                model.UserName = dt.Rows[i]["UserName"].ToString();
                model.MemLoanId = int.Parse(dt.Rows[i]["MemLoanId"].ToString());
                list.Add(model);
            }
            PageList.Pager<RepayDetailedModel> page = new PageList.Pager<RepayDetailedModel>();
            page.Data = list;
            page.PageIndex = PageIndex;
            page.PageSize = PageSize;
            page.PageCount = (int)Math.Ceiling(Convert.ToDecimal(totalCount) / Convert.ToDecimal(PageSize));
            return page;
        }

        /// <summary>
        /// 一次性还清
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool update(RepayDetailedModel model)
        {
            SqlParameter[] parameter = { 
                                            new SqlParameter("@RepayDetailedId",SqlDbType.Int),
                                            new SqlParameter("@PaymentState",SqlDbType.Int)
                                        };
            parameter[0].Value = model.RepayDetailedId;
            parameter[1].Value = model.PaymentState;
            return DbHelperSQL.RunProcedureReturnBool("PROC_RepayDetailedMoney", parameter);
        }

        /// <summary>
        /// 判断支付密码是否正确
        /// </summary>
        /// <param name="Uid"></param>
        /// <param name="Upwd"></param>
        /// <returns></returns>
        public int RepayDetailedPwd(int Uid,int Upwd)
        {
            SqlParameter[] parameter = {
                                       new SqlParameter("@UserId",SqlDbType.Int),
                                       new SqlParameter("@PaymentPwd",SqlDbType.Int)
                                       };
            parameter[0].Value = Uid;
            parameter[1].Value = Upwd;
            SqlDataReader read = DbHelperSQL.RunProcedure("PROC_RepayDetailedPwd", parameter);
            UserrsModel model = new UserrsModel();
            while(read.Read())
            {
                model.UserId = Convert.ToInt32(read["UserId"].ToString());
                model.PaymentPwd = Convert.ToInt32(read["PaymentPwd"].ToString());
            }
            if(model != null )
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }

}
